package com.woniu.sharepark.leasesystem.common.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局捕获异常，只要作用在@RequestMapping方法上所有的异常信息都会被捕获到
 * @author DELL
 * @date 2019/6/18 19:57
 * version v1.0
 */
@ControllerAdvice
public class MyControllerAdvice {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map<String,Object> errorHandler(Exception e){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",-1);
        map.put("msg",e.getMessage());

        return map;
    }

    @ResponseBody
    @ExceptionHandler(value = BussinessException.class)
    public Map<String,Object> errorHandler(BussinessException e){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",e.getCode());
        map.put("msg",e.getMsg());

        return map;
    }
}
